package cloud.shyidong.core.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author syd
 * @Date 2022/10/28
 */
public class CacheUtils {
    
    private final static Map<String, Long> TIME_MAP = new LinkedHashMap<>();
    
    private final static Map<String, Map<String, String>> USER_MAP = new LinkedHashMap<>();
    
    private final static Map<String, Map<String, String>> MENU_MAP = new LinkedHashMap<>();
    
    private final static Map<String, Map<String, String>> FUNCTION_MAP = new LinkedHashMap<>();
    
    private final static Map<String, List<?>> TREE_LIST = new LinkedHashMap<>();
    
    public static void putTime(String key, Integer i) {
        TIME_MAP.put(key, System.currentTimeMillis() + i * 1000 * 60 * 60 * 24);
    }
    
    public static void putUser(String key, Map<String, String> map) {
        putUser(key, map, 1);
    }
    
    public static void putUser(String key, Map<String, String> map, Integer i) {
        USER_MAP.put(key, map);
        putTime(key, i);
    }
    
    public static Map<String, String> getUser(String key) {
        if (TIME_MAP.get(key) == null || System.currentTimeMillis() > TIME_MAP.get(key)) {
            removeMap(key);
            return null;
        }
        if (!USER_MAP.containsKey(key)) {
            removeMap(key);
            return null;
        }
        TIME_MAP.put(key, System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        return USER_MAP.get(key);
    }
    
    public static void putMenu(String key, Map<String, String> map) {
        putMenu(key, map, 1);
    }
    
    public static void putMenu(String key, Map<String, String> map, Integer i) {
        MENU_MAP.put(key, map);
        putTime(key, i);
    }
    
    public static Map<String, String> getMenu(String key) {
        if (TIME_MAP.get(key) == null || System.currentTimeMillis() > TIME_MAP.get(key)) {
            removeMap(key);
            return null;
        }
        if (!MENU_MAP.containsKey(key)) {
            removeMap(key);
            return null;
        }
        TIME_MAP.put(key, System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        return MENU_MAP.get(key);
    }
    
    public static void putFunction(String key, Map<String, String> map) {
        putFunction(key, map, 1);
    }
    
    public static void putFunction(String key, Map<String, String> map, Integer i) {
        FUNCTION_MAP.put(key, map);
        putTime(key, i);
    }
    
    public static Map<String, String> getFunction(String key) {
        if (TIME_MAP.get(key) == null || System.currentTimeMillis() > TIME_MAP.get(key)) {
            removeMap(key);
            return null;
        }
        if (!FUNCTION_MAP.containsKey(key)) {
            removeMap(key);
            return null;
        }
        TIME_MAP.put(key, System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        return FUNCTION_MAP.get(key);
    }
    
    public static void putList(String key, List<?> treeList) {
        putList(key, treeList, 1);
    }
    
    public static void putList(String key, List<?> treeList, Integer i) {
        TREE_LIST.put(key, treeList);
        putTime(key, i);
    }
    
    public static List<?> getList(String key) {
        if (TIME_MAP.get(key) == null || System.currentTimeMillis() > TIME_MAP.get(key)) {
            removeMap(key);
            return null;
        }
        if (!TIME_MAP.containsKey(key)) {
            removeMap(key);
            return null;
        }
        TIME_MAP.put(key, System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        return TREE_LIST.get(key);
    }
    
    public static void removeMap(String key) {
        if (TIME_MAP.containsKey(key)) {
            TIME_MAP.remove(key);
        }
        if (USER_MAP.containsKey(key)) {
            USER_MAP.remove(key);
        }
        if (MENU_MAP.containsKey(key)) {
            MENU_MAP.remove(key);
        }
        if (FUNCTION_MAP.containsKey(key)) {
            FUNCTION_MAP.remove(key);
        }
    }
    
    
}
